var ids = {
    map:        '#map',
    biz:        'biz_'
};
//default Google Map
var map = {};
var markers = {};
var addresses = {};

var MAX_MARKERS = 0;
var BIZ_MAP = null;

//initialize Google Map
function initialize() {
    // GMap
    BIZ_MAP =  new google.maps.Map($(ids['map'])[0], {
        center: new google.maps.LatLng(map.Xa, map.Ya),
        mapTypeId: map.style,
        zoom: parseInt(map.zoom)
    });

    if (jQuery.isEmptyObject(addresses)) {
        // Default GMap
        var id = ids.biz + 0;
        addAutocomplete(id);
        markers[0] = {};
        ITEMS = 1;
    } else {

        // Customize GMap
        jQuery.each(addresses, function(index, value) {
            // Marker
		
            markers[index] = gmarkerCreate(index, value);
            gmarkerSetDefaultProperties(markers[index]);
            //gmarkerAddEvents(markers[index]);

            // Add events on GMap
            //gmapAddEvents();

            /*Utilities*/
            // Is Canvas or Fan Page

            var id = ids.biz + index;
            if ($(id).exists()) {
                addAutocomplete(id);
            }
            // Add value to hidden field
            //updateHiddenField(index, value.Xa, value.Ya);
            // Number of addresses
            MAX_MARKERS = MAX_MARKERS + 1;
        });

        //Set center GMap
        BIZ_MAP.setCenter(new google.maps.LatLng(map.Xa, map.Ya));
    }
}

function addAutocomplete(e) {
    var input = document.getElementById(e);
    var autocomplete = new google.maps.places.Autocomplete(input);
    autocomplete.bindTo('bounds', BIZ_MAP);

    // Add events on Autocomplete
    google.maps.event.addListener(autocomplete, 'place_changed', function() {
        var id = '#' + e;
        editAddress(e, $(id).val());
		
    });
    return autocomplete;
}
function addOnChangeEvent(id) {
    $(id).keypress(function(event) {
        if ( event.which == 13 ) {
            event.preventDefault();
            editAddress(id, $(id).val());
			
        }
    });
}
function editAddress(id, value) {
    var index = getIndex(id);
	console.log(id);
    // Update latlngs and markers
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({'address':value}, function(result, status) {
        if(status == google.maps.GeocoderStatus.OK) {
            gmarkerClear(markers[index]);
            var properties = {
                name: value,
                Xa: result[0].geometry.location.lat(),
                Ya: result[0].geometry.location.lng()
            };
			
            markers[index] = gmarkerCreate(index, properties);
			gmarkerSetDefaultProperties(markers[index]);
            BIZ_MAP.setCenter(result[0].geometry.location);
        } else {
        }
    });
}

function delBiz(e) {    
    var index = getIndex(e.id); 
    var biz_id = ids.biz + index;
    $(biz_id).remove();
    $(e).remove();
    // markers
    if (markers[index] && markers[index].position) {
        // Clear old marker
        markers[index].setMap(null);
        markers[index] = {};    
    }
}
